﻿using System;
using System.Collections.Generic;
using System.Text;
using BlueLotus7.MetaWeblogUtil.Sina.Contract;
using BlueLotus7.MetaWeblogUtil.Sina.Contract.Interfaces;
using BlueLotus7.MetaWeblogUtil.Contract;
using BlueLotus7.MetaWeblogUtil.Contract.Interfaces;
using CookComputing.XmlRpc;

namespace BlueLotus7.MetaWeblogUtil.Sina
{
    public class SinaAgent : BaseAgent<ISinaBlogMetaWeblogProxy>, IMetaWeblogAgent<ISinaBlogMetaWeblogProxy>
    {
        protected override string XmlRpcUrlFormat
        {
            get
            {
                return "http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php";
            }
        }

        public SinaAgent(string loginName, string password)
            : base(string.Empty, loginName, password, string.Empty)
        {
        }

        #region Methods

        protected override void Initialize()
        {
            base.Initialize();
            if (!string.IsNullOrEmpty(XmlRpcUrlFormat))
            {
                this.Proxy.Url = string.Format(XmlRpcUrlFormat, this.XmlRpcUrlIdentity);
            }
        }

        public string AddPost(SinaPost post)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_newPost;
            return Proxy.NewPost(string.Empty, this.LoginName, this.Password, post, true);
        }

        public void UpdatePost(SinaPost post)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_editPost;
            Proxy.EditPost(post.postid, this.LoginName, this.Password, post, true);
        }

        public SinaPost GetPost(string postId)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_getPost;
            return Proxy.GetPost(postId, this.LoginName, this.Password);
        }

        public CategoryInfo[] GetCategories(string blogid)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_getCategories;
            return Proxy.GetCategories(blogid, this.LoginName, this.Password);
        }

        public MediaObjectInfo NewMediaObject(string blogid, MediaObject mediaObject)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_newMediaObject;
            return Proxy.NewMediaObject(blogid, this.LoginName, this.Password, mediaObject);
        }

        #endregion
    }
}
